package cn.edu.dyxy.controller;

import cn.edu.dyxy.entity.CartItem;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * @author JokerQiu
 * @date 2021年12月25日 09:14
 */
@WebServlet("/changeAmount")
public class ChangeAmountServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("ChangeAmountServlet");
        /**
         * 获取数据
         * 修改数据
         * 返回结果
         */
        //获取数据
        String bookId = req.getParameter("bookId");
        String amount = req.getParameter("amount");
        System.out.println("bookId:" + bookId + ",amount:" + amount);
        //获取购物车里的书籍，然后修改数量
        HttpSession session = req.getSession();
        Map<String, CartItem> cart =
                (Map<String, CartItem>) session.getAttribute("cart");
        //通过书籍的id，在购物车中获取
        CartItem cartItem = cart.get(bookId);
        //更改数量
        cartItem.setAmount(Integer.parseInt(amount));
        resp.getWriter().write("修改数量成功");


    }
}




